package com.passwd.designsearch.search;


import com.passwd.designcore.utils.FileUtils;
import com.passwd.designcore.utils.KeyUtils;
import com.passwd.designcore.utils.SMS4;

import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;

public class SearchCore {

    public String searchRun(String content, InputStream inputStream) throws Exception {
        ArrayList<String> arrayList = FileUtils.readFile(inputStream);
        Init init = init(content);
        String result = "success";
        if (arrayList == null || arrayList.isEmpty()) {
            result = "error";
        }
        try {
            String[] keys = arrayList.get(0).split(",|，");
            for (int i = 0; i < keys.length; i++) {
                 result=searchFile(keys[i],init); // 返回 exist 表示存在，返回null，表示不存在
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "error";
        }
        return result;
    }

    /**
     * 搜索 第一二步
     * @param content
     * @return
     * @throws Exception
     */
    public Init init(String content) throws Exception {
        int inLen = 16, ENCRYPT = 1, DECRYPT = 0, inlen1 = 32;
        SMS4 sms4 = new SMS4();
        byte[] c1 = new byte[16];
        byte[] K3 = new byte[16];
        byte[] fiveResult = new byte[16];
        // 第一步
        byte[] key = KeyUtils.fillZeroTo128(content);
        sms4.sms4(key,16,KeyUtils.K1,c1,ENCRYPT);

        // 第二步
        byte[] c1Left64 = new byte[8];
        for (int i = 0; i < c1.length / 2; i++) { // 取c1 的左64
            c1Left64[i] = c1[i];
        }
        byte[] c1Left128 = KeyUtils.fillZeroTo128(c1);// 将c1 的左64 填充至128
        sms4.sms4(c1Left128, 16, KeyUtils.K2, K3, ENCRYPT);// 计算出K3

        return new Init(K3,c1);
    }


    public String searchFile(String C1Key, Init init) {
        int inLen = 16, ENCRYPT = 1, DECRYPT = 0, inlen1 = 32;
        SMS4 sms4 = new SMS4();
        // 第三步
        byte [] C1=new byte[16];
        byte[] T1=new byte[16];
        getBytes(C1Key,C1);// 将key 转成byte左右数组
        for (int i = 0; i < init.c1.length; i++) {
            byte t = (byte)(C1[i] ^ init.c1[i]); // 密文c1和C1异或得到T1
            T1[i]=t;
        }

        // 第四步
        byte[] TLeft64 = new byte[8];
        for (int i = 0; i < T1.length / 2; i++) { // 取T1 的左64（即第一个流密码S1）
            TLeft64[i] = T1[i];
        }
        TLeft64=KeyUtils.fillZeroTo128(TLeft64);
        byte[] result=new byte[16];
        sms4.sms4(TLeft64,16,init.K3,result,ENCRYPT);

        byte[] resultRight64 = new byte[8];
        for (int i = result.length / 2; i < result.length; i++) {// T1后64比特(右)
            resultRight64[i - result.length / 2] = result[i];
        }

        byte[] T1Right64 = new byte[8];
        for (int i = T1.length / 2; i < T1.length; i++) {// T1后64比特(右)
            T1Right64[i - result.length / 2] = T1[i];
        }

        String T1Right = new String(T1Right64);
        String resultRight = new String(resultRight64);
        return T1Right.equals(resultRight)?"exist":"null";
    }

    private void getBytes(String key,  byte[] C1) {
        String[] split = key.split("\\.");

        for (int i = 0; i < split.length; i++) {
            C1[i]=new BigInteger(split[i], 16).byteValue();
        }

    }
}



class Init{
    public Init(byte[] k3, byte[] c1) {
        K3 = k3;
        this.c1 = c1;
    }

    byte[] K3;
    byte[] c1;
}